/* jSSC-Terminal - serial port terminal.
 * © Alexey Sokolov (scream3r), 2011.
 *
 * This file is part of jSSC-Terminal.
 *
 * jSSC-Terminal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jSSC-Terminal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 *
 * e-mail: scream3r.org@gmail.com
 * web-site: www.scream3r.org
 */
package modbusandserialtests.trash;

import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import javax.swing.Icon;

/**
 *
 * @author scream3r
 */
public class DialogMessage extends javax.swing.JPanel {

    public static final int TYPE_ERROR = 0;
    public static final int TYPE_INFO = 1;
    public static final int TYPE_QUESTION = 2;
    public static final int TYPE_WARNING = 3;

    private Icon error = javax.swing.UIManager.getIcon("OptionPane.errorIcon");
    private Icon info = javax.swing.UIManager.getIcon("OptionPane.informationIcon");
    private Icon question = javax.swing.UIManager.getIcon("OptionPane.questionIcon");
    private Icon warning = javax.swing.UIManager.getIcon("OptionPane.warningIcon");

    private Icon[] icons = {error, info, question, warning};

    private Form parent;

    /** Creates new form DialogMessage */
    public DialogMessage(Form parent, int type, String header, String message) {
        this.parent = parent;
        initComponents();
        jHeaderLabel.setText(header);
        jLabelMessage.setIcon(icons[type]);
        jLabelMessage.setText(message);
        setSize(500, 136);

        parent.setControlsFocusable(false);
        GlassPane glassPane = new GlassPane();
        glassPane.add(this);
        int x = (parent.getWidth()/2) - getWidth()/2;
        int y = (parent.getHeight()/2) - getHeight()/2;
        setBounds(x, y, getWidth(), getHeight());
//        Main.getApplet().setGlassPane(glassPane);
//        glassPane.setVisible(true);
    }

    private void closeDialog() {
//        Main.getApplet().getGlassPane().setVisible(false);
//        parent.setControlsFocusable(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jHeaderPanel = new javax.swing.JPanel(){

            protected void paintComponent(Graphics g){
                GradientPaint paint = new GradientPaint(0, 0, NimbusGui.INFO_PANEL_TOP_COLOR, 0, getHeight(), NimbusGui.INFO_PANEL_BOTTOM_COLOR);
                Graphics2D graphics2D = (Graphics2D)g.create();
                graphics2D.setPaint(paint);
                graphics2D.fillRect(0, 0, getWidth(), getHeight());
            }
        }
        ;
        jHeaderLabel = new javax.swing.JLabel();
        jDataPanel = new javax.swing.JPanel();
        jLabelMessage = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonClose = new javax.swing.JButton();

        jHeaderPanel.setBorder(NimbusGui.DIALOG_PANEL_BORDER);
        jHeaderPanel.setPreferredSize(new java.awt.Dimension(286, 30));

        jHeaderLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        jHeaderLabel.setForeground(NimbusGui.SECTION_LABEL_FONT_COLOR);
        jHeaderLabel.setText("Header");

        javax.swing.GroupLayout jHeaderPanelLayout = new javax.swing.GroupLayout(jHeaderPanel);
        jHeaderPanel.setLayout(jHeaderPanelLayout);
        jHeaderPanelLayout.setHorizontalGroup(
            jHeaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jHeaderPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jHeaderLabel)
                .addContainerGap(447, Short.MAX_VALUE))
        );
        jHeaderPanelLayout.setVerticalGroup(
            jHeaderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jHeaderLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
        );

        jDataPanel.setBackground(new java.awt.Color(231, 233, 237));
        jDataPanel.setBorder(NimbusGui.DIALOG_PANEL_BORDER);
        jDataPanel.setPreferredSize(new java.awt.Dimension(276, 75));

        jLabelMessage.setFont(NimbusGui.DEFAULT_FONT);

        jSeparator1.setPreferredSize(new java.awt.Dimension(50, 2));

        jButtonClose.setFont(NimbusGui.DEFAULT_FONT);
        jButtonClose.setText("Close");
        jButtonClose.setPreferredSize(new java.awt.Dimension(90, 28));
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });
        jButtonClose.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonCloseKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jDataPanelLayout = new javax.swing.GroupLayout(jDataPanel);
        jDataPanel.setLayout(jDataPanelLayout);
        jDataPanelLayout.setHorizontalGroup(
            jDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            .addGroup(jDataPanelLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                .addGap(6, 6, 6))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDataPanelLayout.createSequentialGroup()
                .addContainerGap(404, Short.MAX_VALUE)
                .addComponent(jButtonClose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6))
        );
        jDataPanelLayout.setVerticalGroup(
            jDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDataPanelLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabelMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jButtonClose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jHeaderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
            .addComponent(jDataPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jHeaderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jDataPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        closeDialog();
}//GEN-LAST:event_jButtonCloseActionPerformed

    private void jButtonCloseKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonCloseKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ESCAPE){
            closeDialog();
        }
        else if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            jButtonClose.doClick();
        }
}//GEN-LAST:event_jButtonCloseKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JPanel jDataPanel;
    static javax.swing.JLabel jHeaderLabel;
    private javax.swing.JPanel jHeaderPanel;
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
